from primes import erastothenes
from digits import digits, rdigits
from permutations import rotations

limit = 1000000

primes = erastothenes(limit)
allowed = frozenset([1,3,7,9])
candidates = frozenset([p for p in primes if all(d in allowed for d in digits(p))])
circular = set([2, 5])

for p in candidates:
    r = [rdigits(x) for x in rotations(digits(p))]
    if all(n in candidates for n in r):
        circular.update(r)
        
print len(circular)

        
        
        
        
    